package com.BigDate.server.utils;

import org.pegdown.PegDownProcessor;
import org.springframework.util.StringUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 *
 * @author Suntingxing
 * @date 2021/12/27 9:33
 */
public class MarkDownUtils {
    public static String openFile(String filePath) {
        int HttpResult; // 服务器返回的状态
        try
        {
            URL url =new URL(filePath); // 创建URL
            URLConnection urlconn = url.openConnection(); // 试图连接并取得返回状态码
            urlconn.connect();
            HttpURLConnection httpconn =(HttpURLConnection)urlconn;
            HttpResult = httpconn.getResponseCode();
            if(HttpResult != HttpURLConnection.HTTP_OK) {
                System.out.print("无法连接到");
                return null;
            } else {
                int filesize = urlconn.getContentLength(); // 取数据长度
                InputStreamReader isReader = new InputStreamReader(urlconn.getInputStream(),"UTF-8");
                BufferedReader reader = new BufferedReader(isReader);
                StringBuffer buffer = new StringBuffer();
                String line = null;
                String mdContent = "";
                while ((line = reader.readLine()) != null) {
                    mdContent += line + "\r\n";
                }
                PegDownProcessor pdp = new PegDownProcessor(Integer.MAX_VALUE);
                String htmlContent = pdp.markdownToHtml(mdContent);
                System.out.println(htmlContent);
                return htmlContent;
            }
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void downIpynb(String urlPath,String mdPath,String savePath) throws Exception {
        //URL url = new URL(urlPath);
        URL mdUrl = new URL(mdPath);
        //URLConnection conn = url.openConnection();
        URLConnection mdConn = mdUrl.openConnection();

        //InputStream is = conn.getInputStream();
        InputStream mdIs = mdConn.getInputStream();

        //byte[] bytes = new byte[1024];
        byte[] mdBytes = new byte[1024];

        //int len;
        int mdLen;

        File file = new File(savePath);

        //OutputStream os = new FileOutputStream(file.getPath()+"\\1.ipynb");
        OutputStream mdOs = new FileOutputStream(file.getPath()+"\\2.md");
        // 开始下载
//        while ((len = is.read(bytes))!=-1){
//            os.write(bytes,0,len);
//        }
//        os.close();
//        is.close();

        while ((mdLen = mdIs.read(mdBytes))!=-1){
            mdOs.write(mdBytes,0,mdLen);
        }
        mdOs.close();
        mdIs.close();

    }

    public static void resourceGenerator(String ipynbPath,String mdPath,String dateSetPath,String savePath) throws Exception {
        URL ipynbUrl = new URL(ipynbPath);
        URL mdUrl = new URL(mdPath);
        URLConnection ipynbConn = ipynbUrl.openConnection();
        URLConnection mdConn = mdUrl.openConnection();

        InputStream ipynbIs = ipynbConn.getInputStream();
        InputStream mdIs = mdConn.getInputStream();

        byte[] ipynbBytes = new byte[1024];
        byte[] mdBytes = new byte[1024];

        int ipynbLen;
        int mdLen;

        File file = new File(savePath);

        OutputStream ipynbOs = new FileOutputStream(file.getPath()+"\\1.ipynb");
        OutputStream mdOs = new FileOutputStream(file.getPath()+"\\1.md");
        // 开始下载 ipynb文件
        while ((ipynbLen = ipynbIs.read(ipynbBytes))!=-1){
            ipynbOs.write(ipynbBytes,0,ipynbLen);
        }
        ipynbOs.close();
        ipynbIs.close();

        // 开始下载md文件
        while ((mdLen = mdIs.read(mdBytes))!=-1){
            mdOs.write(mdBytes,0,mdLen);
        }
        mdOs.close();
        mdIs.close();


        if(StringUtils.isEmpty(dateSetPath)){
            URL dateSetUrl = new URL(dateSetPath);
            URLConnection dateSetConn = dateSetUrl.openConnection();
            InputStream dateSetIs = dateSetConn.getInputStream();
            byte[] dateSetBytes = new byte[1024];
            int dateSetLen;
            OutputStream dateSetOs = new FileOutputStream(file.getPath()+"\\1.md");
            while ((dateSetLen = dateSetIs.read(dateSetBytes))!=-1){
                dateSetOs.write(dateSetBytes,0,dateSetLen);
            }
            dateSetOs.close();
            dateSetIs.close();


        }


    }
}
